package com.linkai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkai.dto.admin.business.saleReback.SaleOrderRebackIndexDto;
import com.linkai.entity.SaleRebackOrder;
import com.linkai.entity.SaleRebackOrderProduct;
import com.linkai.entity.Storage;
import com.linkai.mapper.*;
import com.linkai.service.SaleRebackOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yamon
 * @since 2021-03-19
 */
@Service
public class SaleRebackOrderServiceImpl extends ServiceImpl<SaleRebackOrderMapper, SaleRebackOrder> implements SaleRebackOrderService {
    @Autowired
    private SaleorderProductMapper saleorderProductMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private MoneyaccountMapper moneyaccountMapper;
    @Autowired
    private StorageMapper storageMapper;

    @Autowired
    private SaleRebackOrderProductMapper saleRebackOrderProductMapper;
    /**
     * 列出所有的销售退货单
     *
     * @return list
     */
    @Override
    public List<SaleOrderRebackIndexDto> listAll() {
        final List<SaleRebackOrder> list = this.list();
        return list.stream().map(saleRebackOrder -> {
            final SaleOrderRebackIndexDto saleOrderRebackIndexDto = new SaleOrderRebackIndexDto();
            saleOrderRebackIndexDto.setId(saleRebackOrder.getId());
            saleOrderRebackIndexDto.setOrderNumber(saleRebackOrder.getSaleOrderNumber());
            saleOrderRebackIndexDto.setEmpName(employeeMapper.selectById(saleRebackOrder.getEmpId()).getName());
            saleOrderRebackIndexDto.setAccName(moneyaccountMapper.selectById(saleRebackOrder.getAccId()).getAccName());
            saleOrderRebackIndexDto.setPrice(saleRebackOrder.getPrice());
            saleOrderRebackIndexDto.setInpState(saleRebackOrder.getState());
            saleOrderRebackIndexDto.setInpDate(saleRebackOrder.getInpDate());
            //TODO:待修改
//            final QueryWrapper<SaleRebackOrderProduct> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("sale_reback_order_id",saleRebackOrder.getId());
//            final List<SaleRebackOrderProduct> saleorderProducts = saleRebackOrderProductMapper.selectList(queryWrapper);
//            saleOrderRebackIndexDto.setProductNum(saleorderProducts.stream().mapToInt(SaleRebackOrderProduct::getProNum).sum());
            return saleOrderRebackIndexDto;
        }).collect(Collectors.toList());
    }

    /**
     * 入库，更新库存
     *
     * @param id
     * @return
     */
    @Override
    public boolean updateStorageBySaleId(Integer id) {
        final SaleRebackOrder byId = this.getById(id);
        byId.setState(1);
        this.updateById(byId);
        final QueryWrapper<SaleRebackOrderProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sale_reback_order_id",id);
        final List<SaleRebackOrderProduct> saleRebackOrderProducts = saleRebackOrderProductMapper.selectList(queryWrapper);
        for (SaleRebackOrderProduct saleRebackOrderProduct : saleRebackOrderProducts) {
            saleRebackOrderProduct.setProState(1);
            saleRebackOrderProductMapper.updateById(saleRebackOrderProduct);
            final Integer proNum = saleRebackOrderProduct.getProNum();
            final BigDecimal eachSum = saleRebackOrderProduct.getEachSum();
            final Integer storageId = saleRebackOrderProduct.getStorageId();
            this.inputStorage(storageId,proNum,eachSum);
        }
        return true;
    }

    /**
     * 更新库存，增加
     * @param storageId 库存id
     * @param proNum 增加的数量
     * @param eachSum 增加的钱
     * @return
     */
    public boolean inputStorage(Integer storageId, Integer proNum, BigDecimal eachSum){
        final Storage storage = storageMapper.selectById(storageId);
        storage.setProNum(storage.getProNum()+proNum);
        storage.setProMoney(storage.getProMoney().add(eachSum));
        return storageMapper.updateById(storage) == 1;
    }
}
